<template>
  <a-modal
    title="订单产品"
    :width="1000"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    :okButtonProps="{ props: { disabled: disableSubmit } }"
    @cancel="handleCancel"
    cancelText="关闭"
    style="top: 20px"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-input placeholder="请输入产品名称" v-model="productName"/>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden; padding-left: 10px;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
      <a-table
        style="margin: 24px 0px 0px 0px"
        rowKey="id"
        bordered
        :scroll="{ x: 800,y:300 }"
        :columns="returnGoodsColumns"
        :pagination="false"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        :dataSource="dataSource">
        <div slot="returnFileList" slot-scope="returnFileList, row">
          <div :key="index" v-for="(file, index) in returnFileList">
            <a :href="file.url" target="_blank">{{ file.name }}</a>
          </div>
        </div>
      </a-table>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from "lodash.pick";
import {httpAction, postAction} from "@api/manage";
import {getAction} from "@api/manage";
import moment from 'moment'
import {JeecgListMixin} from '@/mixins/JeecgListMixin'

export default {
  name: "OrderReturnProduct",
  mixins: [JeecgListMixin],
  data() {
    return {
      visible: false,
      confirmLoading: false,
      disableSubmit: false,
      storeId: '',
      productName: '',
      approveStatus: 5,
      applyId: 0,
      instructionList: [],
      inspectionReport: [],
      form: this.$form.createForm(this),
      validatorRules: {
        status: {rules: [{required: true, message: "请选择状态!"}]},
        note: {rules: [{required: true, message: '请输入拒绝原因!'}]},
      },
      labelCol: {
        xs: {span: 24},
        sm: {span: 8},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 13},
      },
      type: 1,

      dataSource: [],
      returnGoodsColumns: [
        {
          title: '商品名称',
          key: 'name',
          width: 150,
          dataIndex: 'name'
        },
        {
          title: '物料编码',
          key: 'skuCode',
          width: 140,
          dataIndex: 'skuCode'
        },
      ],
      url: {
        list: "/returns/omsOrderReturnApply/getOrderReturnProductList",
        addApplyItem: "/returns/omsOrderReturnItem/addApplyItem",
      },
      loading: false,
    };
  },
  computed: {},
  methods: {
    moment,
    show(storeId,applyId) {
      this.selectedRowKeys =[];
      this.selectionRows =[];
      this.visible = true;
      this.applyId=applyId;
      this.storeId = storeId;
      this.loadGoodsData(1);
    },
    searchQuery() {
      this.loadGoodsData(1);
    },
    loadGoodsData(index) {
      //加载数据 若传入参数1则加载第一页的内容
      if (index === 1) {
        this.ipagination.current = 1;
      }

      let params = {storeId: this.storeId, productName: this.productName};
      params.pageNo = 1;
      params.pageSize = 1000;

      this.loading = true;
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records;
          this.ipagination.total = res.result.total;
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false;
      })
    },
    close() {
      this.form.resetFields();
      this.$emit("close");
      this.visible = false;
    },
    handleSubmit() {
      let params = {};
      if (this.selectedRowKeys && this.selectedRowKeys.length > 0) {
        params.productIds = this.selectedRowKeys.join(",")
      } else {
        this.$message.warning("请选择商品");
        return;
      }
      params.applyId=this.applyId;

      getAction(this.url.addApplyItem, params).then((res) => {
        if (res.success) {
          this.$message.success("新增成功");
          this.$emit("refreshApplyItem");
          this.close();
        }
        else{
          this.$message.warning(res.message);
        }
        this.loading = false;
      })


    },
    handleCancel() {
      this.close();
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectionRows = selectionRows;
    },
  },
};
</script>
<style>

.ant-descriptions-item-label {

  width: 120px;
}

.title {
  margin-bottom: 10px;
  font-weight: bold;
  line-height: 1.5;
}
</style>